package bb.lanxing.util;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import bb.lanxing.App;
import bb.lanxing.R;
import kotlin.jvm.internal.Intrinsics;

public final class DialogUtil {
    private DialogUtil() {
    }

    public static LoadingPopupWindow initProgressDialog(Activity activity, PopupWindow.OnDismissListener onDismissListener) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        LoadingPopupWindow loadingPopupWindow = new LoadingPopupWindow(activity);
        loadingPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
        loadingPopupWindow.setTouchable(true);
        loadingPopupWindow.setOnDismissListener(onDismissListener);
        loadingPopupWindow.setFocusable(true);
        return loadingPopupWindow;
    }

    public static void adjustDialogGravityCenter(Dialog dialog) {
        Window window;
        WindowManager.LayoutParams attributes;
        if (dialog == null || (window = dialog.getWindow()) == null) {
            return;
        }
        Window window2 = dialog.getWindow();
        if (window2 == null || (attributes = window2.getAttributes()) == null) {
            attributes = null;
        } else {
            Intrinsics.checkNotNullExpressionValue(attributes, "attributes");
            attributes.gravity = Gravity.CENTER;
            attributes.width = (int) (attributes.width * 0.9d);
        }
        window.setAttributes(attributes);
    }

    public static void adjustDialogGravity$default(Dialog dialog, int i, int i2, Object obj) {
        if ((i2 & 2) != 0) {
            i = 17;
        }
        adjustDialogGravity(dialog, i);
    }

    public static void adjustDialogGravity(Dialog dialog, int i) {
        Window window;
        WindowManager.LayoutParams attributes;
        if (dialog == null || (window = dialog.getWindow()) == null) {
            return;
        }
        Window window2 = dialog.getWindow();
        if (window2 == null || (attributes = window2.getAttributes()) == null) {
            attributes = null;
        } else {
            Intrinsics.checkNotNullExpressionValue(attributes, "attributes");
            attributes.gravity = i;
            attributes.width = (int) (attributes.width * 0.9d);
        }
        window.setAttributes(attributes);
    }

    public static void showProgressDialog(Activity activity, final LoadingPopupWindow loadingPopupWindow, String str) {
        TextView textView;
        if (activity == null || activity.isFinishing() || loadingPopupWindow == null) {
            return;
        }
        View contentView = loadingPopupWindow.getContentView();
        if (contentView != null && (textView = (TextView) contentView.findViewById(R.id.tv_titlename)) != null && !TextUtils.isEmpty(str)) {
            textView.setText(str);
            textView.setVisibility(View.VISIBLE);
        }
        if (!loadingPopupWindow.isShowing()) {
            final View childAt = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            childAt.post(() -> {
//                try {
//                    DialogUtil.LoadingPopupWindow.showAtLocation(childAt, Gravity.CENTER, 0, 0);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });
        }
    }

    public static void hideDialog(final PopupWindow popupWindow) {
        if (popupWindow != null) {
            popupWindow.getContentView().postDelayed(() -> {
                if (popupWindow.isShowing()) {
                    try {
                        popupWindow.dismiss();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, 200L);
        }
    }

    public static class LoadingPopupWindow extends PopupWindow {
        ImageView loadingLogo;
        ImageView loadingRound;

        public LoadingPopupWindow(Activity activity) {
            super(activity.getLayoutInflater().inflate(R.layout.loading_popup_window, (ViewGroup) null), -1, -1, false);
        }
    }

}